package Song.Han.demo05;

public class BaoZiPu extends Thread{
    //1.需要在成员位置创建一个包子变量
    private BaoZi baoZi;
    //2.使用带参构造
    public BaoZiPu(BaoZi bz){
        this.baoZi = bz;
    }
    @Override
    public void run(){
        //定义一个变量
        int count = 0;
        //一直生产包子
        while (true){
            synchronized (baoZi){
                if (baoZi.flag == true){
                    //已经有包子了，包子铺调用wait方法进入等待
                    try{
                        baoZi.wait();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }

                //被唤醒后执行
                //包子铺开始交替生产两种包子
                if(count%2 == 0){
                    //生产薄皮三鲜
                    baoZi.pi = "薄皮";
                    baoZi.xian = "三鲜";
                }else{
                    //冰皮牛肉
                    baoZi.pi = "冰皮";
                    baoZi.xian = "牛肉";
                }
                count++;
                System.out.println("包子铺正在生产："+baoZi.pi+baoZi.xian+"包子");
                //生产需要3s
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //生产完成,唤醒顾客线程
                baoZi.flag = true;
                baoZi.notify();
                System.out.println(baoZi.pi+baoZi.xian+"包子做好了，可以开始吃了");
            }
        }
    }
}
